﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atom.biz;
using Atom.dao.Model;
using Atom.NetFrame;

namespace Atom.Logic
{
    /// <summary>
    /// 一对一发送
    /// </summary>
    public class AbsOnceHandler
    {
        private byte type;
        private int area;
        public IUserBiz UserBiz = BizFactory.UserBiz;

        public void SetType(byte type)
        {
            this.type = type;
        }
        public virtual byte Gettype()
        {
            return type;
        }

        public void SetArea(int area)
        {
            this.area = area;
        }
        public virtual int GetArea()
        {
            return area;
        }

        /// <summary>
        /// 获取角色模型
        /// </summary>
        /// <param name="ut"></param>
        /// <returns></returns>
        public UserMode GetUser(UserToken ut)
        {
            UserMode user = UserBiz.GetUser(ut);
            if (user == null) return null;
            return user;
        }
        /// <summary>
        /// 获取角色模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserMode GetUser(int id)
        {
            UserMode user = UserBiz.GetUser(id);
            if (user == null) return null;
            return user;
        }
        /// <summary>
        /// 获取角色模型ID
        /// </summary>
        /// <param name="ut"></param>
        /// <returns></returns>
        public int GetUserID(UserToken ut)
        {
            UserMode user = UserBiz.GetUser(ut);
            if (user == null) return -1;
            return GetUser(ut).ID;
        }

        public UserToken GetUserToken(int id)
        {
            UserToken ut = UserBiz.GetToken(id);
            return ut;
        }

        public void SendMsg(UserToken ut, int command)
        {
            SendMsg(ut, command, null);
        }
        public void SendMsg(UserToken ut, int command, byte[] message)
        {
            SendMsg(ut, GetArea(), command, message);
        }
        public void SendMsg(UserToken ut, int area, int command, byte[] message)
        {
            SendMsg(ut, Gettype(), area, command, message);
        }
        public void SendMsg(UserToken ut, byte type, int area, int command, byte[] message)
        {
            try
            {
                SocketModel sm = new SocketModel(type, area, command, message);
                ut.SendMsg(sm.Encode());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }

        public void SendMsg(int id, int command)
        {
            SendMsg(id, command, null);
        }
        public void SendMsg(int id, int command, byte[] message)
        {
            SendMsg(id, GetArea(), command, message);
        }
        public void SendMsg(int id, int area, int command, byte[] message)
        {
            SendMsg(id, Gettype(), area, command, message);
        }
        public void SendMsg(int id, byte type, int area, int command, byte[] message)
        {

            UserToken ut = GetUserToken(id);
            if (ut == null) return;
            SendMsg(ut, type, area, command, message);
        }

        public void SendMsgToUsers(int[] ids, byte type, int area, int command, byte[] message)
        {
            SocketModel sm = new SocketModel(type, area, command, message);
            foreach (int item in ids)
            {      
                UserToken ut = GetUserToken(item);
                ut.SendMsg(sm.Encode());
            }
        }
    }
}
